package com.tencent.smtt.sdk;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.tencent.smtt.sdk.TbsDownloadConfig;
import com.tencent.smtt.sdk.TbsLogReport;
import com.tencent.smtt.utils.Apn;
import com.tencent.smtt.utils.TbsLog;
import java.io.File;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;

/* loaded from: classes2.dex */
public class TbsDownloader {
    public static final boolean DEBUG_DISABLE_DOWNLOAD = false;
    public static boolean DOWNLOAD_OVERSEA_TBS = false;
    public static final String LOGTAG = "TbsDownload";
    static boolean a = false;
    private static String b = null;
    private static Context c = null;
    private static Handler d = null;
    private static String e = null;
    private static Object f = new byte[0];
    private static ag g = null;
    private static HandlerThread h = null;
    private static boolean i = false;
    private static boolean j = false;
    private static boolean k = false;
    private static long l = -1;

    /* loaded from: classes2.dex */
    public interface TbsDownloaderCallback {
        void onNeedDownloadFinish(boolean z, int i);
    }

    public static File a(int i2) {
        String str;
        String[] coreProviderAppList = TbsShareManager.getCoreProviderAppList();
        int length = coreProviderAppList.length;
        File file = null;
        int i3 = 0;
        while (true) {
            if (i3 >= length) {
                break;
            }
            String str2 = coreProviderAppList[i3];
            if (!str2.equals(c.getApplicationInfo().packageName)) {
                file = new File(com.tencent.smtt.utils.j.a(c, str2, 4, false), getOverSea(c) ? "x5.oversea.tbs.org" : "x5.tbs.org");
                if (!file.exists()) {
                    str = "can not find local backup core file";
                } else if (com.tencent.smtt.utils.a.a(c, file) == i2) {
                    TbsLog.i(LOGTAG, "local tbs version fond,path = " + file.getAbsolutePath());
                    break;
                } else {
                    str = "version is not match";
                }
                TbsLog.i(LOGTAG, str);
            }
            i3++;
        }
        return file;
    }

    /* JADX WARN: Removed duplicated region for block: B:76:0x0068  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x008e  */
    /* JADX WARN: Removed duplicated region for block: B:88:0x0090  */
    /* JADX WARN: Removed duplicated region for block: B:91:0x009c  */
    /* JADX WARN: Removed duplicated region for block: B:93:0x00a5  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static java.lang.String a(android.content.Context r6) {
        /*
            java.lang.String r6 = "ISO8859-1"
            java.lang.String r0 = "UTF-8"
            java.lang.String r1 = com.tencent.smtt.sdk.TbsDownloader.b
            boolean r1 = android.text.TextUtils.isEmpty(r1)
            if (r1 != 0) goto Lf
            java.lang.String r6 = com.tencent.smtt.sdk.TbsDownloader.b
            return r6
        Lf:
            java.util.Locale r1 = java.util.Locale.getDefault()
            java.lang.StringBuffer r2 = new java.lang.StringBuffer
            r2.<init>()
            java.lang.String r3 = android.os.Build.VERSION.RELEASE
            java.lang.String r4 = new java.lang.String     // Catch: java.lang.Exception -> L25
            byte[] r5 = r3.getBytes(r0)     // Catch: java.lang.Exception -> L25
            r4.<init>(r5, r6)     // Catch: java.lang.Exception -> L25
            r3 = r4
            goto L26
        L25:
        L26:
            java.lang.String r4 = "1.0"
            if (r3 != 0) goto L2e
        L2a:
            r2.append(r4)
            goto L37
        L2e:
            int r5 = r3.length()
            if (r5 <= 0) goto L2a
            r2.append(r3)
        L37:
            java.lang.String r3 = "; "
            r2.append(r3)
            java.lang.String r4 = r1.getLanguage()
            if (r4 == 0) goto L59
            java.lang.String r4 = r4.toLowerCase()
            r2.append(r4)
            java.lang.String r1 = r1.getCountry()
            if (r1 == 0) goto L5e
            java.lang.String r4 = "-"
            r2.append(r4)
            java.lang.String r1 = r1.toLowerCase()
            goto L5b
        L59:
            java.lang.String r1 = "en"
        L5b:
            r2.append(r1)
        L5e:
            java.lang.String r1 = android.os.Build.VERSION.CODENAME
            java.lang.String r4 = "REL"
            boolean r1 = r4.equals(r1)
            if (r1 == 0) goto L88
            java.lang.String r1 = android.os.Build.MODEL
            java.lang.String r4 = new java.lang.String     // Catch: java.lang.Exception -> L75
            byte[] r0 = r1.getBytes(r0)     // Catch: java.lang.Exception -> L75
            r4.<init>(r0, r6)     // Catch: java.lang.Exception -> L75
            r1 = r4
            goto L76
        L75:
        L76:
            if (r1 != 0) goto L7c
            r2.append(r3)
            goto L88
        L7c:
            int r6 = r1.length()
            if (r6 <= 0) goto L88
            r2.append(r3)
            r2.append(r1)
        L88:
            java.lang.String r6 = android.os.Build.ID
            java.lang.String r0 = ""
            if (r6 != 0) goto L90
            r6 = r0
            goto L92
        L90:
            java.lang.String r6 = android.os.Build.ID
        L92:
            java.lang.String r1 = "[一-龥]"
            java.lang.String r6 = r6.replaceAll(r1, r0)
            java.lang.String r0 = " Build/"
            if (r6 != 0) goto La5
            r2.append(r0)
            java.lang.String r6 = "00"
        La1:
            r2.append(r6)
            goto Laf
        La5:
            int r1 = r6.length()
            if (r1 <= 0) goto Laf
            r2.append(r0)
            goto La1
        Laf:
            r6 = 1
            java.lang.Object[] r6 = new java.lang.Object[r6]
            r0 = 0
            r6[r0] = r2
            java.lang.String r0 = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 Mobile Safari/533.1"
            java.lang.String r6 = java.lang.String.format(r0, r6)
            com.tencent.smtt.sdk.TbsDownloader.b = r6
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.smtt.sdk.TbsDownloader.a(android.content.Context):java.lang.String");
    }

    private static String a(String str) {
        return str == null ? "" : str;
    }

    private static void a(boolean z, TbsDownloaderCallback tbsDownloaderCallback) {
        TbsLog.i(LOGTAG, "[TbsDownloader.queryConfig]");
        d.removeMessages(100);
        Message obtain = Message.obtain(d, 100);
        if (tbsDownloaderCallback != null) {
            obtain.obj = tbsDownloaderCallback;
        }
        obtain.arg1 = 0;
        obtain.arg1 = z ? 1 : 0;
        obtain.sendToTarget();
    }

    private static boolean a(Context context, boolean z, TbsDownloaderCallback tbsDownloaderCallback) {
        int i2;
        TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(context);
        if (Build.VERSION.SDK_INT < 8) {
            i2 = -102;
        } else if (!QbSdk.c && TbsShareManager.isThirdPartyApp(c) && !c()) {
            if (tbsDownloaderCallback != null) {
                tbsDownloaderCallback.onNeedDownloadFinish(false, 0);
            }
            return false;
        } else {
            if (!tbsDownloadConfig.mPreferences.contains(TbsDownloadConfig.TbsConfigKey.KEY_IS_OVERSEA)) {
                if (z && !TbsConfig.APP_WX.equals(context.getApplicationInfo().packageName)) {
                    TbsLog.i(LOGTAG, "needDownload-oversea is true, but not WX");
                    z = false;
                }
                tbsDownloadConfig.a.put(TbsDownloadConfig.TbsConfigKey.KEY_IS_OVERSEA, Boolean.valueOf(z));
                tbsDownloadConfig.commit();
                j = z;
                TbsLog.i(LOGTAG, "needDownload-first-called--isoversea = " + z);
            }
            if (!getOverSea(context) || Build.VERSION.SDK_INT == 16 || Build.VERSION.SDK_INT == 17 || Build.VERSION.SDK_INT == 18) {
                Matcher matcher = null;
                String string = tbsDownloadConfig.mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_DEVICE_CPUABI, null);
                e = string;
                if (TextUtils.isEmpty(string)) {
                    return true;
                }
                try {
                    matcher = Pattern.compile("i686|mips|x86_64").matcher(e);
                } catch (Exception unused) {
                }
                if (matcher == null || !matcher.find()) {
                    return true;
                }
                if (tbsDownloaderCallback != null) {
                    tbsDownloaderCallback.onNeedDownloadFinish(false, 0);
                }
                i2 = -104;
            } else {
                TbsLog.i(LOGTAG, "needDownload- return false,  because of  version is " + Build.VERSION.SDK_INT + ", and overea");
                if (tbsDownloaderCallback != null) {
                    tbsDownloaderCallback.onNeedDownloadFinish(false, 0);
                }
                i2 = -103;
            }
        }
        tbsDownloadConfig.setDownloadInterruptCode(i2);
        return false;
    }

    /* JADX WARN: Code restructure failed: missing block: B:87:0x014e, code lost:
        if (r9.equals(r5) != false) goto L35;
     */
    /* JADX WARN: Removed duplicated region for block: B:96:0x01c1  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private static boolean a(android.content.Context r20, boolean r21, boolean r22) {
        /*
            Method dump skipped, instructions count: 487
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.smtt.sdk.TbsDownloader.a(android.content.Context, boolean, boolean):boolean");
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:379:0x011d
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:81)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:47)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:39)
        */
    private static boolean a(java.lang.String r33, int r34, boolean r35, boolean r36) {
        /*
            Method dump skipped, instructions count: 1883
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.smtt.sdk.TbsDownloader.a(java.lang.String, int, boolean, boolean):boolean");
    }

    public static File b(int i2) {
        StringBuilder sb;
        String[] coreProviderAppList = TbsShareManager.getCoreProviderAppList();
        int length = coreProviderAppList.length;
        File file = null;
        int i3 = 0;
        while (i3 < length) {
            String str = coreProviderAppList[i3];
            File file2 = new File(com.tencent.smtt.utils.j.a(c, str, 4, false), getOverSea(c) ? "x5.oversea.tbs.org" : "x5.tbs.org");
            if (file2.exists() && com.tencent.smtt.utils.a.a(c, file2) == i2) {
                sb = new StringBuilder();
            } else {
                file2 = new File(com.tencent.smtt.utils.j.a(c, str, 4, false), "x5.tbs.decouple");
                if (file2.exists() && com.tencent.smtt.utils.a.a(c, file2) == i2) {
                    sb = new StringBuilder();
                } else {
                    i3++;
                    file = file2;
                }
            }
            sb.append("local tbs version fond,path = ");
            sb.append(file2.getAbsolutePath());
            TbsLog.i(LOGTAG, sb.toString());
            return file2;
        }
        return file;
    }

    private static JSONObject b(boolean z, boolean z2, boolean z3) {
        int n;
        TbsLog.i(LOGTAG, "[TbsDownloader.postJsonData]isQuery: " + z + " forDecoupleCore is " + z3);
        TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(c);
        String a2 = a(c);
        String d2 = com.tencent.smtt.utils.b.d(c);
        String c2 = com.tencent.smtt.utils.b.c(c);
        String f2 = com.tencent.smtt.utils.b.f(c);
        String id = TimeZone.getDefault().getID();
        String str = id != null ? id : "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) c.getSystemService("phone");
            if (telephonyManager != null) {
                id = telephonyManager.getSimCountryIso();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        String str2 = id != null ? id : "";
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("TIMEZONEID", str);
            jSONObject.put("COUNTRYISO", str2);
            jSONObject.put("PROTOCOLVERSION", 1);
            if (TbsShareManager.isThirdPartyApp(c)) {
                n = QbSdk.c ? TbsShareManager.a(c, false) : TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0);
            } else {
                n = am.a().n(c);
                if (n == 0 && am.a().m(c)) {
                    n = -1;
                }
                TbsLog.i(LOGTAG, "[TbsDownloader.postJsonData] tbsLocalVersion=" + n + " isDownloadForeground=" + z2);
                if (z2 && !am.a().m(c)) {
                    n = 0;
                }
            }
            if (z) {
                jSONObject.put("FUNCTION", 2);
            } else {
                jSONObject.put("FUNCTION", n == 0 ? 0 : 1);
            }
            if (TbsShareManager.isThirdPartyApp(c)) {
                JSONArray f3 = f();
                jSONObject.put("TBSVLARR", f3);
                tbsDownloadConfig.a.put(TbsDownloadConfig.TbsConfigKey.KEY_LAST_THIRDAPP_SENDREQUEST_COREVERSION, f3.toString());
                tbsDownloadConfig.commit();
                if (QbSdk.c) {
                    jSONObject.put("THIRDREQ", 1);
                }
            } else {
                JSONArray h2 = h();
                if (Apn.getApnType(c) != 3 && h2.length() != 0 && n == 0 && z) {
                    jSONObject.put("TBSBACKUPARR", h2);
                }
            }
            jSONObject.put("APPN", c.getPackageName());
            jSONObject.put("APPVN", a(tbsDownloadConfig.mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_APP_VERSIONNAME, null)));
            jSONObject.put("APPVC", tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_APP_VERSIONCODE, 0));
            jSONObject.put("APPMETA", a(tbsDownloadConfig.mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_APP_METADATA, null)));
            jSONObject.put("TBSSDKV", 43612);
            jSONObject.put("TBSV", n);
            jSONObject.put("DOWNLOADDECOUPLECORE", z3 ? 1 : 0);
            tbsDownloadConfig.a.put(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOADDECOUPLECORE, Integer.valueOf(z3 ? 1 : 0));
            tbsDownloadConfig.commit();
            if (n != 0) {
                jSONObject.put("TBSBACKUPV", g.b());
            }
            jSONObject.put("CPU", e);
            jSONObject.put("UA", a2);
            jSONObject.put("IMSI", a(d2));
            jSONObject.put("IMEI", a(c2));
            jSONObject.put("ANDROID_ID", a(f2));
            if (!TbsShareManager.isThirdPartyApp(c)) {
                if (n != 0) {
                    jSONObject.put("STATUS", QbSdk.a(c, n) ? 0 : 1);
                } else {
                    jSONObject.put("STATUS", 0);
                }
                jSONObject.put("TBSDV", am.a().i(c));
            }
            boolean z4 = TbsDownloadConfig.getInstance(c).mPreferences.getBoolean(TbsDownloadConfig.TbsConfigKey.KEY_FULL_PACKAGE, false);
            Object a3 = QbSdk.a(c, "can_unlzma", (Bundle) null);
            if ((a3 == null || !(a3 instanceof Boolean)) ? false : ((Boolean) a3).booleanValue() ? !z4 : false) {
                jSONObject.put("REQUEST_LZMA", 1);
            }
            if (getOverSea(c)) {
                jSONObject.put("OVERSEA", 1);
            }
            if (z2) {
                jSONObject.put("DOWNLOAD_FOREGROUND", 1);
            }
        } catch (Exception unused) {
        }
        TbsLog.i(LOGTAG, "[TbsDownloader.postJsonData] jsonData=" + jSONObject.toString());
        return jSONObject;
    }

    public static void b(Context context) {
        TbsDownloadConfig.getInstance(context).clear();
        TbsLogReport.a(context).d();
        ag.c(context);
        (Build.VERSION.SDK_INT >= 11 ? context.getSharedPreferences("tbs_extension_config", 4) : context.getSharedPreferences("tbs_extension_config", 0)).edit().clear().commit();
        (Build.VERSION.SDK_INT >= 11 ? context.getSharedPreferences("tbs_preloadx5_check_cfg_file", 4) : context.getSharedPreferences("tbs_preloadx5_check_cfg_file", 0)).edit().clear().commit();
    }

    public static boolean b(boolean z, boolean z2) {
        return c(z, z2, false);
    }

    private static boolean c() {
        try {
            for (String str : TbsShareManager.getCoreProviderAppList()) {
                if (TbsShareManager.getSharedTbsCoreVersion(c, str) > 0) {
                    return true;
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return false;
    }

    /* JADX WARN: Can't wrap try/catch for region: R(25:14|(1:16)(1:104)|17|(1:19)(1:103)|20|(1:22)(1:102)|23|(1:25)|26|(2:28|(1:30)(2:31|(1:33)(2:34|(1:36))))|37|(1:39)|40|(4:42|43|44|(9:48|(3:50|(1:52)(1:95)|53)(1:(1:97)(1:98))|54|55|56|57|(4:84|(3:86|(1:88)|89)|90|(1:92))|60|(5:62|63|(1:72)|73|75)(1:83)))|101|55|56|57|(0)|84|(0)|90|(0)|60|(0)(0)) */
    /* JADX WARN: Code restructure failed: missing block: B:178:0x0174, code lost:
        r10 = -1;
     */
    /* JADX WARN: Removed duplicated region for block: B:185:0x018b  */
    /* JADX WARN: Removed duplicated region for block: B:191:0x01fc A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:212:0x01ff A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:218:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static boolean c(boolean r22, boolean r23, boolean r24) {
        /*
            Method dump skipped, instructions count: 641
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.smtt.sdk.TbsDownloader.c(boolean, boolean, boolean):boolean");
    }

    private static synchronized void d() {
        synchronized (TbsDownloader.class) {
            if (h == null) {
                h = al.a();
                try {
                    g = new ag(c);
                    d = new aj(h.getLooper());
                } catch (Exception unused) {
                    i = true;
                    TbsLog.e(LOGTAG, "TbsApkDownloader init has Exception");
                }
            }
        }
    }

    private static boolean e() {
        try {
            return TbsDownloadConfig.getInstance(c).mPreferences.getString(TbsDownloadConfig.TbsConfigKey.KEY_LAST_THIRDAPP_SENDREQUEST_COREVERSION, "").equals(f().toString());
        } catch (Exception unused) {
            return false;
        }
    }

    private static JSONArray f() {
        String[] coreProviderAppList;
        boolean z;
        StringBuilder sb;
        boolean z2;
        boolean z3;
        boolean z4;
        if (TbsShareManager.isThirdPartyApp(c)) {
            JSONArray jSONArray = new JSONArray();
            boolean z5 = true;
            if (QbSdk.getOnlyDownload()) {
                coreProviderAppList = new String[]{c.getApplicationContext().getPackageName()};
            } else {
                coreProviderAppList = TbsShareManager.getCoreProviderAppList();
                String packageName = c.getApplicationContext().getPackageName();
                if (packageName.equals(TbsShareManager.f(c))) {
                    int length = coreProviderAppList.length;
                    String[] strArr = new String[length + 1];
                    System.arraycopy(coreProviderAppList, 0, strArr, 0, length);
                    strArr[length] = packageName;
                    coreProviderAppList = strArr;
                }
            }
            for (String str : coreProviderAppList) {
                int sharedTbsCoreVersion = TbsShareManager.getSharedTbsCoreVersion(c, str);
                if (sharedTbsCoreVersion > 0) {
                    Context packageContext = TbsShareManager.getPackageContext(c, str);
                    if (packageContext == null || am.a().f(packageContext)) {
                        int i2 = 0;
                        while (true) {
                            if (i2 >= jSONArray.length()) {
                                z4 = false;
                                break;
                            } else if (jSONArray.optInt(i2) == sharedTbsCoreVersion) {
                                z4 = true;
                                break;
                            } else {
                                i2++;
                            }
                        }
                        if (!z4) {
                            jSONArray.put(sharedTbsCoreVersion);
                        }
                    } else {
                        TbsLog.e(LOGTAG, "host check failed,packageName = " + str);
                    }
                }
            }
            for (String str2 : coreProviderAppList) {
                int coreShareDecoupleCoreVersion = TbsShareManager.getCoreShareDecoupleCoreVersion(c, str2);
                if (coreShareDecoupleCoreVersion > 0) {
                    Context packageContext2 = TbsShareManager.getPackageContext(c, str2);
                    if (packageContext2 == null || am.a().f(packageContext2)) {
                        int i3 = 0;
                        while (true) {
                            if (i3 >= jSONArray.length()) {
                                z3 = false;
                                break;
                            } else if (jSONArray.optInt(i3) == coreShareDecoupleCoreVersion) {
                                z3 = true;
                                break;
                            } else {
                                i3++;
                            }
                        }
                        if (!z3) {
                            jSONArray.put(coreShareDecoupleCoreVersion);
                        }
                    } else {
                        TbsLog.e(LOGTAG, "host check failed,packageName = " + str2);
                    }
                }
            }
            if (!QbSdk.mDisableUseHostBackupCore) {
                for (String str3 : coreProviderAppList) {
                    int backupCoreVersion = TbsShareManager.getBackupCoreVersion(c, str3);
                    if (backupCoreVersion > 0) {
                        Context packageContext3 = TbsShareManager.getPackageContext(c, str3);
                        if (packageContext3 == null || am.a().f(packageContext3)) {
                            int i4 = 0;
                            while (true) {
                                if (i4 >= jSONArray.length()) {
                                    z2 = false;
                                    break;
                                } else if (jSONArray.optInt(i4) == backupCoreVersion) {
                                    z2 = true;
                                    break;
                                } else {
                                    i4++;
                                }
                            }
                            if (!z2) {
                                jSONArray.put(backupCoreVersion);
                            }
                        } else {
                            sb = new StringBuilder();
                            sb.append("host check failed,packageName = ");
                            sb.append(str3);
                            TbsLog.e(LOGTAG, sb.toString());
                        }
                    }
                    int backupDecoupleCoreVersion = TbsShareManager.getBackupDecoupleCoreVersion(c, str3);
                    if (backupDecoupleCoreVersion > 0) {
                        Context packageContext4 = TbsShareManager.getPackageContext(c, str3);
                        if (packageContext4 == null || am.a().f(packageContext4)) {
                            int i5 = 0;
                            while (true) {
                                if (i5 >= jSONArray.length()) {
                                    z = false;
                                    break;
                                } else if (jSONArray.optInt(i5) == backupDecoupleCoreVersion) {
                                    z = true;
                                    break;
                                } else {
                                    i5++;
                                }
                            }
                            if (!z) {
                                jSONArray.put(backupDecoupleCoreVersion);
                            }
                        } else {
                            sb = new StringBuilder();
                            sb.append("host check failed,packageName = ");
                            sb.append(str3);
                            TbsLog.e(LOGTAG, sb.toString());
                        }
                    }
                }
            }
            if (TbsShareManager.getHostCorePathAppDefined() != null) {
                int a2 = am.a().a(TbsShareManager.getHostCorePathAppDefined());
                int i6 = 0;
                while (true) {
                    if (i6 >= jSONArray.length()) {
                        z5 = false;
                        break;
                    } else if (jSONArray.optInt(i6) == a2) {
                        break;
                    } else {
                        i6++;
                    }
                }
                if (!z5) {
                    jSONArray.put(a2);
                }
            }
            return jSONArray;
        }
        return null;
    }

    private static boolean g() {
        int i2;
        TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(c);
        if (tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_SUCCESS_RETRYTIMES, 0) >= tbsDownloadConfig.getDownloadSuccessMaxRetrytimes()) {
            TbsLog.i(LOGTAG, "[TbsDownloader.needStartDownload] out of success retrytimes", true);
            i2 = -115;
        } else if (tbsDownloadConfig.mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOAD_FAILED_RETRYTIMES, 0) >= tbsDownloadConfig.getDownloadFailedMaxRetrytimes()) {
            TbsLog.i(LOGTAG, "[TbsDownloader.needStartDownload] out of failed retrytimes", true);
            i2 = -116;
        } else if (com.tencent.smtt.utils.j.b(c)) {
            if (System.currentTimeMillis() - tbsDownloadConfig.mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_STARTTIME, 0L) <= org.achartengine.internal.chart.j.b) {
                long j2 = tbsDownloadConfig.mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_TBSDOWNLOAD_FLOW, 0L);
                TbsLog.i(LOGTAG, "[TbsDownloader.needStartDownload] downloadFlow=" + j2);
                if (j2 >= tbsDownloadConfig.getDownloadMaxflow()) {
                    TbsLog.i(LOGTAG, "[TbsDownloader.needStartDownload] failed because you exceeded max flow!", true);
                    i2 = -120;
                }
            }
            return true;
        } else {
            TbsLog.i(LOGTAG, "[TbsDownloader.needStartDownload] local rom freespace limit", true);
            i2 = -117;
        }
        tbsDownloadConfig.setDownloadInterruptCode(i2);
        return false;
    }

    public static int getCoreShareDecoupleCoreVersion() {
        return am.a().i(c);
    }

    public static int getCoreShareDecoupleCoreVersionByContext(Context context) {
        return am.a().i(context);
    }

    public static synchronized boolean getOverSea(Context context) {
        boolean z;
        synchronized (TbsDownloader.class) {
            if (!k) {
                k = true;
                TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(context);
                if (tbsDownloadConfig.mPreferences.contains(TbsDownloadConfig.TbsConfigKey.KEY_IS_OVERSEA)) {
                    j = tbsDownloadConfig.mPreferences.getBoolean(TbsDownloadConfig.TbsConfigKey.KEY_IS_OVERSEA, false);
                    TbsLog.i(LOGTAG, "[TbsDownloader.getOverSea]  first called. sOverSea = " + j);
                }
                TbsLog.i(LOGTAG, "[TbsDownloader.getOverSea]  sOverSea = " + j);
            }
            z = j;
        }
        return z;
    }

    public static long getRetryIntervalInSeconds() {
        return l;
    }

    public static HandlerThread getsTbsHandlerThread() {
        return h;
    }

    private static JSONArray h() {
        boolean z;
        JSONArray jSONArray = new JSONArray();
        for (String str : TbsShareManager.getCoreProviderAppList()) {
            File file = new File(com.tencent.smtt.utils.j.a(c, str, 4, false), getOverSea(c) ? "x5.oversea.tbs.org" : "x5.tbs.org");
            if (file.exists()) {
                long a2 = com.tencent.smtt.utils.a.a(c, file);
                if (a2 > 0) {
                    int i2 = 0;
                    while (true) {
                        if (i2 >= jSONArray.length()) {
                            z = false;
                            break;
                        } else if (jSONArray.optInt(i2) == a2) {
                            z = true;
                            break;
                        } else {
                            i2++;
                        }
                    }
                    if (!z) {
                        jSONArray.put(a2);
                    }
                }
            }
        }
        return jSONArray;
    }

    public static boolean isDownloadForeground() {
        ag agVar = g;
        return agVar != null && agVar.e();
    }

    public static synchronized boolean isDownloading() {
        boolean z;
        synchronized (TbsDownloader.class) {
            TbsLog.i(LOGTAG, "[TbsDownloader.isDownloading] is " + a);
            z = a;
        }
        return z;
    }

    public static boolean needDownload(Context context, boolean z) {
        return needDownload(context, z, false, null);
    }

    public static boolean needDownload(Context context, boolean z, boolean z2, TbsDownloaderCallback tbsDownloaderCallback) {
        boolean contains;
        boolean z3;
        int i2;
        StringBuilder sb;
        String str;
        TbsLog.i(LOGTAG, "[TbsDownloader.needDownload] oversea=" + z + ",isDownloadForeground=" + z2);
        TbsLog.initIfNeed(context);
        if (am.b) {
            if (tbsDownloaderCallback != null) {
                tbsDownloaderCallback.onNeedDownloadFinish(false, 0);
            }
            sb = new StringBuilder();
            str = "[TbsDownloader.needDownload]#1,return ";
        } else {
            TbsLog.app_extra(LOGTAG, context);
            Context applicationContext = context.getApplicationContext();
            c = applicationContext;
            TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(applicationContext);
            tbsDownloadConfig.setDownloadInterruptCode(-100);
            if (a(c, z, tbsDownloaderCallback)) {
                d();
                if (!i) {
                    boolean a2 = a(c, z2, false);
                    TbsLog.i(LOGTAG, "[TbsDownloader.needDownload],needSendRequest=" + a2);
                    if (a2) {
                        a(z2, tbsDownloaderCallback);
                        tbsDownloadConfig.setDownloadInterruptCode(-114);
                    }
                    d.removeMessages(102);
                    Message.obtain(d, 102).sendToTarget();
                    if (QbSdk.c || !TbsShareManager.isThirdPartyApp(context)) {
                        contains = tbsDownloadConfig.mPreferences.contains(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD);
                        TbsLog.i(LOGTAG, "[TbsDownloader.needDownload] hasNeedDownloadKey=" + contains);
                        z3 = (contains || TbsShareManager.isThirdPartyApp(context)) ? tbsDownloadConfig.mPreferences.getBoolean(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD, false) : true;
                    } else {
                        z3 = false;
                        contains = false;
                    }
                    TbsLog.i(LOGTAG, "[TbsDownloader.needDownload]#4,needDownload=" + z3 + ",hasNeedDownloadKey=" + contains);
                    if (!z3) {
                        int n = am.a().n(c);
                        TbsLog.i(LOGTAG, "[TbsDownloader.needDownload]#7,tbsLocalVersion=" + n + ",needSendRequest=" + a2);
                        if (a2 || n <= 0) {
                            d.removeMessages(103);
                            ((n > 0 || a2) ? Message.obtain(d, 103, 1, 0, c) : Message.obtain(d, 103, 0, 0, c)).sendToTarget();
                            i2 = -121;
                        } else {
                            i2 = -119;
                        }
                        tbsDownloadConfig.setDownloadInterruptCode(i2);
                    } else if (g()) {
                        tbsDownloadConfig.setDownloadInterruptCode(-118);
                        TbsLog.i(LOGTAG, "[TbsDownloader.needDownload]#6");
                    } else {
                        TbsLog.i(LOGTAG, "[TbsDownloader.needDownload]#5,set needDownload = false");
                        z3 = false;
                    }
                    if (!a2 && tbsDownloaderCallback != null) {
                        tbsDownloaderCallback.onNeedDownloadFinish(false, 0);
                    }
                    TbsLog.i(LOGTAG, "[TbsDownloader.needDownload] needDownload=" + z3);
                    return z3;
                }
                if (tbsDownloaderCallback != null) {
                    tbsDownloaderCallback.onNeedDownloadFinish(false, 0);
                }
                tbsDownloadConfig.setDownloadInterruptCode(-105);
                sb = new StringBuilder();
                str = "[TbsDownloader.needDownload]#3,return ";
            } else {
                sb = new StringBuilder();
                str = "[TbsDownloader.needDownload]#2,return ";
            }
        }
        sb.append(str);
        sb.append(false);
        TbsLog.i(LOGTAG, sb.toString());
        return false;
    }

    public static boolean needDownloadDecoupleCore() {
        int i2;
        if (TbsShareManager.isThirdPartyApp(c) || TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOADDECOUPLECORE, 0) == 1) {
            return false;
        }
        return System.currentTimeMillis() - TbsDownloadConfig.getInstance(c).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_LAST_DOWNLOAD_DECOUPLE_CORE, 0L) >= TbsDownloadConfig.getInstance(c).getRetryInterval() * 1000 && (i2 = TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, 0)) > 0 && i2 != am.a().i(c) && TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0) != i2;
    }

    public static boolean needSendRequest(Context context, boolean z) {
        c = context.getApplicationContext();
        TbsLog.initIfNeed(context);
        boolean z2 = false;
        if (a(c, z, (TbsDownloaderCallback) null)) {
            int n = am.a().n(context);
            TbsLog.i(LOGTAG, "[TbsDownloader.needSendRequest] localTbsVersion=" + n);
            if (n > 0) {
                return false;
            }
            if (a(c, false, true)) {
                return true;
            }
            TbsDownloadConfig tbsDownloadConfig = TbsDownloadConfig.getInstance(c);
            boolean contains = tbsDownloadConfig.mPreferences.contains(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD);
            TbsLog.i(LOGTAG, "[TbsDownloader.needSendRequest] hasNeedDownloadKey=" + contains);
            boolean z3 = !contains ? true : tbsDownloadConfig.mPreferences.getBoolean(TbsDownloadConfig.TbsConfigKey.KEY_NEEDDOWNLOAD, false);
            TbsLog.i(LOGTAG, "[TbsDownloader.needSendRequest] needDownload=" + z3);
            if (z3 && g()) {
                z2 = true;
            }
            TbsLog.i(LOGTAG, "[TbsDownloader.needSendRequest] ret=" + z2);
            return z2;
        }
        return false;
    }

    public static void setAppContext(Context context) {
        if (context == null || context.getApplicationContext() == null) {
            return;
        }
        c = context.getApplicationContext();
    }

    public static void setRetryIntervalInSeconds(Context context, long j2) {
        if (context == null) {
            return;
        }
        if (context.getApplicationInfo().packageName.equals("com.tencent.qqlive")) {
            l = j2;
        }
        TbsLog.i(LOGTAG, "mRetryIntervalInSeconds is " + l);
    }

    public static boolean startDecoupleCoreIfNeeded() {
        StringBuilder sb;
        int i2;
        TbsLog.i(LOGTAG, "startDecoupleCoreIfNeeded ");
        if (TbsShareManager.isThirdPartyApp(c)) {
            return false;
        }
        TbsLog.i(LOGTAG, "startDecoupleCoreIfNeeded #1");
        if (TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DOWNLOADDECOUPLECORE, 0) == 1) {
            return false;
        }
        TbsLog.i(LOGTAG, "startDecoupleCoreIfNeeded #2");
        long j2 = TbsDownloadConfig.getInstance(c).mPreferences.getLong(TbsDownloadConfig.TbsConfigKey.KEY_LAST_DOWNLOAD_DECOUPLE_CORE, 0L);
        if (System.currentTimeMillis() - j2 < TbsDownloadConfig.getInstance(c).getRetryInterval() * 1000) {
            return false;
        }
        TbsLog.i(LOGTAG, "startDecoupleCoreIfNeeded #3");
        int i3 = TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_DECOUPLECOREVERSION, 0);
        if (i3 <= 0 || i3 == am.a().i(c)) {
            sb = new StringBuilder();
            sb.append("startDecoupleCoreIfNeeded no need, deCoupleCoreVersion is ");
            sb.append(i3);
            sb.append(" getTbsCoreShareDecoupleCoreVersion is ");
            i2 = am.a().i(c);
        } else if (TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0) != i3 || TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V_TYPE, 0) == 1) {
            TbsLog.i(LOGTAG, "startDecoupleCoreIfNeeded #4");
            a = true;
            d.removeMessages(108);
            Message obtain = Message.obtain(d, 108, QbSdk.m);
            obtain.arg1 = 0;
            obtain.sendToTarget();
            TbsDownloadConfig.getInstance(c).a.put(TbsDownloadConfig.TbsConfigKey.KEY_LAST_DOWNLOAD_DECOUPLE_CORE, Long.valueOf(System.currentTimeMillis()));
            return true;
        } else {
            sb = new StringBuilder();
            sb.append("startDecoupleCoreIfNeeded no need, KEY_TBS_DOWNLOAD_V is ");
            sb.append(TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V, 0));
            sb.append(" deCoupleCoreVersion is ");
            sb.append(i3);
            sb.append(" KEY_TBS_DOWNLOAD_V_TYPE is ");
            i2 = TbsDownloadConfig.getInstance(c).mPreferences.getInt(TbsDownloadConfig.TbsConfigKey.KEY_TBS_DOWNLOAD_V_TYPE, 0);
        }
        sb.append(i2);
        TbsLog.i(LOGTAG, sb.toString());
        return false;
    }

    public static void startDownload(Context context) {
        startDownload(context, false);
    }

    public static synchronized void startDownload(Context context, boolean z) {
        synchronized (TbsDownloader.class) {
            if (context != null) {
                if (TbsConfig.APP_WX.equals(context.getApplicationInfo().packageName)) {
                    TbsLogReport.TbsLogInfo a2 = TbsLogReport.a(c).a();
                    a2.setErrorCode(126);
                    TbsLogReport.a(c).a(TbsLogReport.EventType.TYPE_DOWNLOAD, a2);
                }
            }
            TbsLog.i(LOGTAG, "[TbsDownloader.startDownload] sAppContext=" + c);
            if (am.b) {
                return;
            }
            int i2 = 1;
            a = true;
            Context applicationContext = context.getApplicationContext();
            c = applicationContext;
            TbsDownloadConfig.getInstance(applicationContext).setDownloadInterruptCode(-200);
            if (Build.VERSION.SDK_INT < 8) {
                QbSdk.m.onDownloadFinish(110);
                TbsDownloadConfig.getInstance(c).setDownloadInterruptCode(-201);
                return;
            }
            d();
            if (i) {
                QbSdk.m.onDownloadFinish(121);
                TbsDownloadConfig.getInstance(c).setDownloadInterruptCode(-202);
                return;
            }
            if (z) {
                stopDownload();
            }
            d.removeMessages(101);
            d.removeMessages(100);
            Message obtain = Message.obtain(d, 101, QbSdk.m);
            if (!z) {
                i2 = 0;
            }
            obtain.arg1 = i2;
            obtain.sendToTarget();
        }
    }

    public static void stopDownload() {
        if (i) {
            return;
        }
        TbsLog.i(LOGTAG, "[TbsDownloader.stopDownload]");
        ag agVar = g;
        if (agVar != null) {
            agVar.c();
        }
        Handler handler = d;
        if (handler != null) {
            handler.removeMessages(100);
            d.removeMessages(101);
            d.removeMessages(108);
        }
    }
}
